<?php

namespace app\admin\model\clothmaking_plan;

use think\Model;


class Index extends Model
{

    
    // 表名
    protected $name = 'clothmaking_plan';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'product_id'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getStatusList()
    {
        return ['normal' => __('Normal'), 'completed' => __('Completed'), 'notstarted' => __('NoStart')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getTypeList()
    {
        return ['product' => __('TypeProduct'), 'sample' => __('TypeSample'),'raw' => __('TypeRaw')];
    }



    public function product()
    {
        return $this->belongsTo('app\admin\model\clothmaking\Product', 'product_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function productBind()
    {
        return $this->belongsTo('app\admin\model\clothmaking\Product', 'productid')->bind(['product_name'=>'name','product_alias'=>'alias']);
    }

    public function recipes(){
        return $this->belongsTo('app\admin\model\ClothmakingRecipe','recipe_id')->setEagerlyType(0);
    }
}
